//
//  HighScoresLayer.m
//  IconPair
//
//  Created by Yangss on 2010-6-26.
//  Copyright 2010 LotusVille. All rights reserved.
//
//	logs:
//	2010-6-27	Yangss	增加背景图，将reset和back按钮换成图片

#import "HighScoresLayer.h"
#import "User.h"
#import "Record.h"
#import "mainpage.h"


@implementation HighScoresLayer
-(id) init{
	self = [super init];
	if (!self) {
		return nil;
	}
			
	//排行榜背景图
	CCSprite *scoreBg = [CCSprite spriteWithFile:@"scoreBg.png"];
	scoreBg.anchorPoint = CGPointZero;
	[self addChild:scoreBg z:0 tag:1];

	//排行榜
	NSArray *scoreRecords = [Record getHighScores];
	for (int i=0; i<MIN([scoreRecords count],kMaxRecordCount); i++) {
		Record *each = [scoreRecords objectAtIndex:i];

		CCLabel *labelNo = [CCLabel labelWithString:[NSString stringWithFormat:@"%d.", i+1] fontName:@"Marker Felt" fontSize:30];
		labelNo.position = ccp(20, 350-30*i);
		[self addChild:labelNo z:2 tag:11];

		CCLabel *labelName = [CCLabel labelWithString:each.name fontName:@"Marker Felt" fontSize:30];
		labelName.position = ccp(120, 350-30*i);
		[self addChild:labelName z:2 tag:12];

		CCLabel *labelValue = [CCLabel labelWithString:[NSString stringWithFormat:@"%d",each.score] fontName:@"Marker Felt" fontSize:30];
		labelValue.position = ccp(250, 350-30*i);
		[self addChild:labelValue z:2 tag:13];
	}

	[CCMenuItemFont setFontName: @"Marker Felt"];
	[CCMenuItemFont setFontSize:28];

	//重置排行榜按钮
	CCSprite * spReset = [CCSprite spriteWithFile:@"scoreBgReset.png"];
	CCMenuItem * miReset = [CCMenuItemSprite itemFromNormalSprite:spReset selectedSprite:spReset target:self selector:@selector(resetCallback:)];
	CCMenu *menuReset = [CCMenu menuWithItems:miReset, nil];
	menuReset.position = ccp(270,30);
	[self addChild:menuReset];
	
	//退回按钮
	CCSprite * spBack = [CCSprite spriteWithFile:@"scoreBgMenu.png"];
	CCMenuItem * miBack = [CCMenuItemSprite itemFromNormalSprite:spBack selectedSprite:spBack target:self selector:@selector(backCallback:)];
	CCMenu *menuBack = [CCMenu menuWithItems:miBack, nil];
	menuBack.position = ccp(60,30);
	[self addChild:menuBack];

	return self;
}

//重置排行榜回调函数
-(void) resetCallback: (id) sender
{
	[Record tryClearScoreRecord];
//下面这行什么用？我注释掉了			－－－－－Michael
// 会导致崩溃
	
//	[self init];
}

//返回按钮回调函数
-(void) backCallback: (id) sender
{
	CCScene *sc = [CCScene node];
	[sc addChild:[mainpage node]];
	
	[[CCDirector sharedDirector] replaceScene:  [CCZoomFlipXTransition transitionWithDuration:0.5f scene:sc]];	
//	[[CCDirector sharedDirector] popScene];
}
@end

